package com.ferry.wallpaper.controller;

import com.ferry.wallpaper.domain.Wall;
import com.ferry.wallpaper.domain.WallType;
import com.ferry.wallpaper.domain.view.UserView;
import com.ferry.wallpaper.domain.view.WallView;
import com.ferry.wallpaper.service.WallService;
import com.ferry.wallpaper.service.WallTypeService;
import com.ferry.wallpaper.service.impl.ThreeWallView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
public class ViewController {

    @Autowired
    private WallService wallService;

    @Autowired
    private WallTypeService wallTypeService;

    @GetMapping("/view")
    public String page(@RequestParam(required = false,defaultValue = "1") Integer index, Model model) {
        List<Wall> page = wallService.getPage(null, 12, index);
        List<WallView> ops = new ArrayList<>();
        for (Wall wall : page) {
            ops.add(new WallView(wall));
        }
        List<ThreeWallView> wallViews = new ArrayList<>();
        for (int i = 0; i < ops.size(); i+=3) {
            ThreeWallView threeWallView = new ThreeWallView();
            threeWallView.setOne(ops.get(i));

            if (i + 1 >= ops.size()) threeWallView.setTwo(null);
            else threeWallView.setTwo(ops.get(i + 1));

            if (i + 2 >= ops.size()) threeWallView.setThree(null);
            else threeWallView.setThree(ops.get(i + 2));

            wallViews.add(threeWallView);
        }
        UserView userView = new UserView();

        List<WallType> all = wallTypeService.getAll();
        userView.setWallTypes(all);
        userView.setWallViews(wallViews);
        userView.setNext("/view/?index="+(index+1));
        userView.setFore("/view/?index="+(index-1>0?index-1:1));
        model.addAttribute("userView",userView);
        return "view";
    }

    @GetMapping("/view/{pageIndex}/{typeName}")
    public String page(@PathVariable(required = false) String typeName,
                       @PathVariable Integer pageIndex, Model model) {
        List<Wall> page = wallService.getPage(typeName, 12, pageIndex);
        List<WallView> ops = new ArrayList<>();
        for (Wall wall : page) {
            ops.add(new WallView(wall));
        }

        List<ThreeWallView> wallViews = new ArrayList<>();
        for (int i = 0; i < ops.size(); i+=3) {
            ThreeWallView threeWallView = new ThreeWallView();
            threeWallView.setOne(ops.get(i));

            if (i + 1 >= ops.size()) threeWallView.setTwo(null);
            else threeWallView.setTwo(ops.get(i + 1));

            if (i + 2 >= ops.size()) threeWallView.setThree(null);
            else threeWallView.setThree(ops.get(i + 2));

            wallViews.add(threeWallView);
        }

        UserView userView = new UserView();

        List<WallType> all = wallTypeService.getAll();
        userView.setWallTypes(all);
        userView.setWallViews(wallViews);
        userView.setNext("/view/"+(pageIndex+1)+(typeName==null?"":"/"+typeName));
        userView.setFore("/view/"+(pageIndex-1>=1?pageIndex-1:pageIndex)+(typeName==null?"":"/"+typeName));

        model.addAttribute("userView",userView);
        return "view";
    }

}
